package com.sh.config;

import com.sh.enums.OssTypeEnum;
import com.sh.service.store.impl.AliOSSService;
import com.sh.service.store.IOSService;
import com.sh.service.store.impl.MinioService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Getter
@Configuration
public class OssClientConfig {

    //    public static String resourceType;
//
//    @Value("${sh.oss.type}")
//    public void   setResourceType(String resourceType){
//        this.resourceType=resourceType;
//    }
    @Value("${sh.oss.type}")
    private String resourceType;

    @Value("${sh.oss.endpoint}")
    private String endpoint;

    @Value("${sh.oss.bucket}")
    private String bucket;

    @Value("${sh.oss.accessKey}")
    private String accessKey;

    @Value("${sh.oss.secretKey}")
    private String secretKey;

    @Value("${sh.oss.signExpires}")
    private Integer signExpires;

    @Bean
    public IOSService ossService() {
        if (OssTypeEnum.MINIO.getName().equals(resourceType)) {
            log.info("初始化MinioService");
            return new MinioService(this);
        } else if (OssTypeEnum.OSS.getName().equals(resourceType)) {
            log.info("初始化OSSService");
            return new AliOSSService(this);
        } else {
            log.warn("不支持的对象存储类型:{}", resourceType);
            throw new UnsupportedOssTypeException(resourceType);
        }
    }

    public class UnsupportedOssTypeException extends RuntimeException {
        public UnsupportedOssTypeException(String resourceType) {
            super("不支持的对象存储类型: " + resourceType);
        }
    }

}
